﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
public partial class admin_SysUserOrFun : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState.Add("SysUserLoginName", Session["SysUserLoginName"].ToString());
            this.tvUserFun.Attributes.Add("onclick", "CheckEvent()");
            LoadTree();
            lblUserName.Text = Session["SysUserName"].ToString();
            
        }
    }
    /// <summary>
    /// 加载 树
    /// </summary>
    private void LoadTree()
    {
        DataTable dt1 = SysFunDAL.Instance.GetFunForIsDir("1");  //根目录的
        DataTable dt2 = SysFunDAL.Instance.GetFunForIsDir("0");  //不是根目录的
        DataTable dt3 = SysUserFunDAL.Instance.GetUserFunForLoginName(ViewState["SysUserLoginName"].ToString());
        TreeNode node = null;
        TreeNode node2 = null;
        bool isChecked = false;
        for(int i = 0; i < dt1.Rows.Count;i++)
        {
            node = new TreeNode(dt1.Rows[i]["name"].ToString(),dt1.Rows[i]["id"].ToString());
            for (int j = 0; j < dt2.Rows.Count; j++)
            {
                if (dt2.Rows[j]["parent_id"].ToString() == dt1.Rows[i]["id"].ToString())
                {
                    node2 = new TreeNode(dt2.Rows[j]["name"].ToString(), dt2.Rows[j]["id"].ToString());
                    for (int n = 0; n < dt3.Rows.Count; n++)
                    {
                        if (dt3.Rows[n]["fun_id"].ToString() == dt2.Rows[j]["id"].ToString())
                        {
                            node2.Checked = true;
                            isChecked = true;
                            break;
                        }
                    }
                    node.ChildNodes.Add(node2);
                }
                if (isChecked)
                {
                    for (int n = 0; n < dt3.Rows.Count; n++)
                    {
                        if (dt3.Rows[n]["fun_id"].ToString() == dt1.Rows[i]["id"].ToString())
                        {
                            node.Checked = true;
                            break;
                        }
                    }
                }
                isChecked = false;
            }
            tvUserFun.Nodes.Add(node);
        }
        
    }
    /// <summary>
    /// 保存用户菜单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string funIds = "";
        TreeNodeCollection nodes = tvUserFun.CheckedNodes;
        for (int i = 0; i < nodes.Count; i++)
        {
            funIds += nodes[i].Value + "#";
        }
        if (SysUserFunDAL.Instance.SaveUserFun(ViewState["SysUserLoginName"].ToString(), funIds.TrimEnd('#')))
        {
            Alert("保存成功!");
            ExecuteScript("window.close();");
        }
        else
        {
            Alert("保存失败!");
        }
    }
}